<?php

/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $email
 * @property string $password
 * @property string $fullname
 * @property integer $gender
 * @property string $birthday
 * @property string $avatar
 * @property integer $role
 * @property string $code
 * @property integer $status
 */
class User extends CActiveRecord
{
    public $repassword;
    public $oldpassword;
    const MALE = 1;
    const FEMALE = 2;
    const ADMIN = 1;
    const MEMBER = 2;
    const STATUS_ACTIVED = 1;
    const STATUS_INACTIVED = 0;
    
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'user';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
            // Validate
            array('email', 'unique', 'message' => '{attribute} này đã tồn tại'),
			array('email, fullname', 'required',
                'message' => '{attribute} không được để trống'),
			array('email', 'length', 'max'=>100),
            array('email', 'email', 'message' => '{attribute} phải đúng định dạng'),
			array('fullname', 'length', 'max'=>50, 'min'=>5, 'tooShort'=>'{attribute} từ 5-50 kí tự'),
            array('fullname', 'ext.alpha', 'allowSpaces'=>true, 'accentedLetters'=>true,
                'message'=>'{attribute} gồm các kí tự chữ cái'),
            array('password, repassword', 'required', 'on'=>array('create','changepassword'),
                'message'=>'{attribute} không được để trống'),
            array('oldpassword','required','on'=>'changepassword','message'=>'{attribute} không được để trống'),
            array('oldpassword','length','max'=>32,'min'=>6,'tooShort'=>'{attribute} từ 6-32 kí tự','on'=>'changepassword'),
            array('password, repassword', 'length', 'max'=>32,'min'=>6, 'tooShort'=>'{attribute} từ 6-32 kí tự'),
            array('repassword', 'compare', 'compareAttribute'=>'password', 'message'=>'{attribute} không trùng khớp'),
            array('avatar', 'file', 'allowEmpty'=>true, 'types'=>'jpg, jpeg, gif, png', 'maxSize'=>410000,
                 'message'=>'{attribute} có dạng jpg, jpeg, gif, png',
                 'tooLarge'=>'{attribute} không quá 400KB'),
            // Filters
            array('email', 'filter', 'filter'=>'strtolower'),
            array('email, fullname', 'filter', 'filter'=>'trim'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, email, fullname, gender, role, status', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
            'article'=>array(self::HAS_MANY,'Article','id'),
            'product'=>array(self::HAS_MANY,'Product','id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'email' => 'Email',
            'oldpassword'=> 'Mật khẩu cũ',
			'password' => 'Mật khẩu',
            'repassword' => 'Nhập lại mật khẩu',
			'fullname' => 'Tên đầy đủ',
			'gender' => 'Giới tính',
			'birthday' => 'Ngày sinh',
			'avatar' => 'Ảnh đại diện',
			'role' => 'Quyền hạn',
			'code' => 'Code',
			'status' => 'Trạng thái',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('email',$this->email,true);
		$criteria->compare('fullname',$this->fullname,true);
		$criteria->compare('gender',$this->gender);
		$criteria->compare('birthday',$this->birthday,true);
		$criteria->compare('avatar',$this->avatar,true);
		$criteria->compare('role',$this->role);
		$criteria->compare('status',$this->status);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
            'sort'=>array(
                'defaultOrder'=>'id DESC',
            ),
            'pagination' => array(
                'pageSize' => 40,
            ),
		));
	}
    
    public function getGenderArray($label = '') {
        if($label) $genderArray[''] = $label;
        $genderArray[self::MALE] = 'Nam';
        $genderArray[self::FEMALE] = 'Nữ';
        return $genderArray;
    }
    
    public function getGenderLabel($gender) {
        $genderArray = array(
            self::MALE => 'Nam',
            self::FEMALE => 'Nữ',
        );
        return $genderArray[$gender];
    }
    
    public function getRoleUserArray($label = '') {
        if($label) $roleArray[''] = $label;
        $roleArray[self::ADMIN] = 'Quản trị';
        $roleArray[self::MEMBER] = 'Thành viên';
        return $roleArray;
    }
    
    public function getRoleUserLabel($role) {
        $roleArray = array(
            self::ADMIN => 'Quản trị',
            self::MEMBER => 'Thành viên',
        );
        return $roleArray[$role];
    }
    
    public function getStatusArray($label = '') {
        if($label) $statusArray[''] = $label;
        $statusArray[self::STATUS_ACTIVED] = 'Hoạt động';
        $statusArray[self::STATUS_INACTIVED] = 'Khóa';
        return $statusArray;
    }
    
    public function getAvatarImage($avatar) {
        $imageUrl = Yii::app()->request->baseUrl.'/data/avatar/';
        if($avatar == '0')
            $imageUrl .= 'no-avatar.gif';
        else
            $imageUrl .= $avatar;
        return $imageUrl;
    }
}